package com.thread;

public class Deadlocker implements Runnable {
	static String i = new String();
	static String j = new String();
	private String str = null;

	public Deadlocker(String str) {
		this.str = str;
	}

	public static void main(String[] args) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					for(int i = 0; i < 10; i++) {
						for(int j = 0; j < 1000; j++) {
							String[] strs = new String[1000];
							Thread.sleep(1);
						}
					}
					/*Object lock = new Object();  
			        synchronized (lock) {  
			            lock.wait();  
			        } */
				} catch(InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		}).start();
		new Thread(new Deadlocker("i")).start();
		new Thread(new Deadlocker("j")).start();
	}

	public void run() {
		if (str.equals("i")) {
			synchronized (i) {
				System.out
						.println(Thread.currentThread().getName() + ":" + "i");
				Thread.yield();
				synchronized (j) {
					System.out.println("i is over");
				}
			}
		} else {
			synchronized (j) {
				System.out
						.println(Thread.currentThread().getName() + ":" + "j");
				Thread.yield();
				synchronized (i) {
					System.out.println("j is over");
				}
			}
		}
	}
}
